'use strict';

// signup controller
app.controller('SignupFormController', ['$scope', '$http', '$state','$interval','AccountService','dbUtils', function ($scope, $http, $state,$interval,accountService,dbUtils) {
    $scope.sendtext = "发送验证码";

    $scope.$watch("rePassword", function () {
        if ($scope.rePassword != $scope.password) {
            $scope.rePasswordInvalid = true;
        } else {
            $scope.rePasswordInvalid = false;
        }
    });

    $scope.sendCode = function(){
        $scope.sending = true;
        accountService.sendCode({phone:$scope.phone,type:0}).then(function (response) {
            if (response.code == 0) {
                var second = 60, timePromise = undefined;
                timePromise = $interval(function(){
                    if(second <= 0){
                        $interval.cancel(timePromise);
                        $scope.sending = undefined;
                        second = 60;
                        $scope.sendtext = "重发验证码";
                    }else{
                        $scope.sendtext = second + "秒后可重发";
                        second--;

                    }
                },1000,100);
            } else {
                $scope.sending = undefined;
                dbUtils.error(response.message,"提示");
            }
        }, function (fail) {
            //服务器出错
        });

    }

    $scope.register = function(){
        console.log("in");
        accountService.register({
            phone: $scope.phone,
            code: $scope.code,
            password: $scope.password
        }).then(function (response) {
            console.log(response);
            if (response.code == 0) {
                dbUtils.success(response.message,"提示");
                $state.go("access.apply");
            } else {
                $scope.sending = undefined;
                dbUtils.error(response.message,"提示");
            }
        }, function (fail) {
            //服务器出错
        });
    }
}])
;